package best.driver.android.Utils;

import android.os.*;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class LoggerSave {

    /**
     * 默认文件夹
     */
    static String BASE_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath() + "/best";
    /**
     * Log文件保存地址
     */
    static String LOG_FILE = BASE_FOLDER + "/log.txt";
    static String LOG_DETAIL_FOLDER = BASE_FOLDER + "/LogDetail";
    public static final Handler WRITE_HANDLER = new Handler(new HandlerThread("write_log") {{
        start();
    }}.getLooper()) {
        FileWriter mWriter;
        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String filePath = bundle.getString("filePath");
            String fileName = bundle.getString("fileName");
            String log = bundle.getString("log");
            if(filePath==null) {
                return;
            }
            File folder =new File(filePath);
            if(!folder.exists()) {
                folder.mkdirs();
            }
            File file = new File(folder, fileName);
            try {
                mWriter = new FileWriter(file, true);
            } catch (IOException e) {
                e.printStackTrace();
                mWriter = null;
            }
            if (mWriter == null) {
                return;
            }
            try {
                mWriter.write(MD_HMS.format(new Date()) + " : " + log + '\n');
                mWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    final static SimpleDateFormat MD_HMS = new SimpleDateFormat("MM-dd HH:mm:ss");
    final static String TAG = "cbh_push";

    static void asyncWriteLog(String filePath, String fileName, String log) {
        Message msg = WRITE_HANDLER.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putString("filePath", filePath);
        bundle.putString("fileName", fileName);
        bundle.putString("log", log);
        msg.setData(bundle);
        WRITE_HANDLER.sendMessage(msg);
    }

    public static void vDetail(String fileName, String msg, Object... arg) {
        String v = Thread.currentThread().getId() + " " + String.format(msg, arg);
        String filePath;
        if(! TextUtils.isEmpty(fileName)) {
            filePath = LOG_DETAIL_FOLDER;
            fileName = fileName+".txt";
        } else {
            filePath = BASE_FOLDER;
            fileName = "log.txt";
        }
        asyncWriteLog(filePath, fileName, v);
    }

    public static void v(String msg, Object... arg) {
        String v = Thread.currentThread().getId() + " " + String.format(msg, arg);
        String filePath = BASE_FOLDER;
        String fileName = "log.txt";
        asyncWriteLog(filePath, fileName, v);
    }

    public static void exception(Throwable throwable) {
        v(Log.getStackTraceString(throwable));
    }
}
